package com.school.sports.controller;


import com.school.sports.entity.Rank;
import com.school.sports.service.RankService;
import com.school.sports.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
@RequestMapping("/rank")
public class RankController {

    @Autowired
    private RankService rankService;

    /**
     * 根据项目ID查询排名及关联信息
     */
    @GetMapping("/project")
    public Result<List<Rank>> getRankByProjectId(@RequestParam(name = "projectId", required = false) Long projectId) {
        List<Rank> rankList = rankService.getRankWithRelations(projectId);
        return rankList != null ? Result.success(rankList) : Result.fail("查询排名失败");
    }

    /**
     * 查询所有排名及关联信息
     */
    @GetMapping("/all")
    public Result<List<Rank>> getAllRank() {
        List<Rank> rankList = rankService.getAllRankWithRelations();
        return rankList != null ? Result.success(rankList) : Result.fail("查询排名失败");
    }

    /**
     * 根据ID查询单条排名及关联信息
     */
    @GetMapping("/{id}")
    public Result<Rank> getRankById(@PathVariable("id") Long id) {
        Rank rank = rankService.getRankByIdWithRelations(id);
        return rank != null ? Result.success(rank) : Result.fail("排名不存在");
    }

}